﻿// ______________________________________________________________
//
// Fly animation class - Liam O'Donnell
// Makes a movieclip whizz around kind of like a fly, so there
// ______________________________________________________________
//

// ______________________________________________________________
//
// USAGE:
// - Create/attach a movieclip that's an instance of Fly
// - You can alternatively use my static method 'create'
// ______________________________________________________________
//

import com.spikything.utils.SpikyTools;

class com.spikything.anim.Fly extends MovieClip {

	// ______________________________________________________
	//
	// CONSTANTS
	// ______________________________________________________
	//

	private var tension 	:Number;
	private var friction 	:Number;
	private var vx 			:Number;
	private var vy 			:Number;
	private var tx 			:Number;
	private var ty 			:Number;
	private var sx 			:Number;
	private var sy 			:Number;

	public 	var spread 		:Number;
	public 	var rand 		:Number;
	
	// ______________________________________________________
	//
	// CONSTRUCTOR
	// ______________________________________________________
	//

	function Fly () {}

	// ______________________________________________________
	//
	// PUBLIC METHODS
	// ______________________________________________________
	//

	public function onLoad ():Void {
		init();
	}

	// ______________________________________________________
	//
	// PRIVATE METHODS
	// ______________________________________________________
	//

	private function init ():Void {
		tension = tension==undefined 	? 1/16 			: tension;
		friction = friction==undefined 	? .9 			: friction;
		spread = spread==undefined 		? 100 			: spread;
		rand = rand==undefined 			? 5 			: rand;
		vx = vx==undefined 				? random(19)-9 	: vx;
		vy = vy==undefined 				? random(19)-9 	: vy;
		sx = _x;
		sy = _y;
		reTarget();
		onEnterFrame = update;
	}

	private function update ():Void {
		if (!random(rand)) reTarget();
		vx += (tx-_x)*tension;
		vy += (ty-_y)*tension;
		vx *= friction;
		vy *= friction;
		_x += vx;
		_y += vy;
	}
	
	private function reTarget ():Void {
		tx = random((spread*2)+1)-spread+sx;
		ty = random((spread*2)+1)-spread+sy;
	}

	// ______________________________________________________
	//
	// STATIC METHODS
	// ______________________________________________________
	//

	public static function create (_base:MovieClip,_id:String,_initObj:Object,_depth:Number):Fly {
		return SpikyTools.attachClass(_base,_id,Fly,_initObj,null,_depth);
	}

}